package com.example.administrator.im.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.im.R;
import com.example.administrator.im.adapter.LinkmanManagerAdapter;
import com.example.administrator.im.callback.MyItemTouchHelperCallback;
import com.example.administrator.im.db.GroupDBHelper;
import com.example.administrator.im.interfacz.ShareTitleable;
import com.example.administrator.im.json.ResultJson;
import com.example.administrator.im.utils.DialogUtils;
import com.example.administrator.im.utils.HttpUtils;
import com.example.administrator.im.view.ShareTitle;
import com.google.gson.Gson;

import org.litepal.LitePal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * create:2018/10/2 16:15
 * author: 姚鸿杰
 **/
public class LinkmanManageActivity extends AppCompatActivity {

    private final String TAG = "LinkmanManageActivity";


    private ShareTitle shareTitle;

    private List<String> title;
    private RecyclerView rv_manage;
    private LinkmanManagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linkman_manage);

        shareTitle = (ShareTitle) findViewById(R.id.manage_toolbar);
        shareTitle.setIconOnClickListener(new ShareTitleable() {
            @Override
            public void left_click(View v) {
                finish();
            }

            @Override
            public void right_click(View v) {
                add_group();
            }
        });

        //从本地数据库获取分组并按照序号升序排序
        List<GroupDBHelper> groups = LitePal.order("position asc").find(GroupDBHelper.class);
        int count = groups.size();
        if (count != 0) {
            title = new ArrayList<>();
            for (int i = 0; i < count; i++)
                title.add(groups.get(i).getName());

            rv_manage = (RecyclerView) findViewById(R.id.rv_linkman_manage);
            rv_manage.setLayoutManager(new LinearLayoutManager(this));
            adapter = new LinkmanManagerAdapter(title, this);
            rv_manage.setAdapter(adapter);
            //条目触目帮助类
            ItemTouchHelper.Callback callback = new MyItemTouchHelperCallback(adapter, this);
            ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
            itemTouchHelper.attachToRecyclerView(rv_manage);
        }
    }

    public void add_group() {

        LayoutInflater inflater = this.getLayoutInflater();
        View view_custom = inflater.inflate(R.layout.dialog_add_group, null, false);

        AlertDialog.Builder builder = new AlertDialog.Builder(this)
                .setView(view_custom);

        final AlertDialog alert = builder.create();
        alert.show();

        final EditText et_name = view_custom.findViewById(R.id.dialog_et_name);
        view_custom.findViewById(R.id.dialog_no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        view_custom.findViewById(R.id.dialog_yes).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();

                DialogUtils.setDialog(LinkmanManageActivity.this, R.layout.loading);
                DialogUtils.showDialog();

                //添加分组操作
                final String name = et_name.getText().toString();
                if (!TextUtils.isEmpty(name)) {

                    //web端添加分组
                    final int position = LitePal.findAll(GroupDBHelper.class).size();
                    HttpUtils.addGroupRequest(position, name, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.e(TAG, e.getMessage());
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            DialogUtils.clearAlerDialog();

                            String content = response.body().string();
                            Gson gson = new Gson();
                            ResultJson json = gson.fromJson(content, ResultJson.class);
                            if (json.getCode() == HttpUtils.SUCCESS) {

                                //本地数据库添加分组
                                GroupDBHelper group = new GroupDBHelper();
                                group.setName(name);
                                group.setPosition(position);
                                group.setDefault_group(false);
                                group.save();

                                //刷新界面
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        adapter.addGroup(name);
                                    }
                                });


                            } else if (json.getCode() == HttpUtils.SUCCESS_NO_CONTENT) {
                                //添加失败提示
                                Log.e(TAG, json.getData().toString());
                            }
                        }
                    });

                } else {
                    Toast.makeText(LinkmanManageActivity.this, "分组名字不能为空!!!", Toast.LENGTH_SHORT).show();
                    DialogUtils.clearAlerDialog();
                    return;
                }
            }
        });

    }
}
